﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.ImgNews.Business;
using gdlmo.DNN.Modules.Article.Business;

namespace gdlmo.DNN.Modules.ImgNews
{
	public abstract class ImgNewsEdit : PortalModuleBase
	{

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Controls
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.LinkButton cmdDelete;
		#endregion
		protected System.Web.UI.WebControls.TextBox txtTitle;
		protected System.Web.UI.WebControls.CheckBox chkNewWindow;
		protected System.Web.UI.WebControls.TextBox txtAuthor;
		protected System.Web.UI.WebControls.TextBox txtLinks;
		protected System.Web.UI.WebControls.TextBox txtCreateDate;
		protected DotNetNuke.UI.UserControls.UrlControl ctlIcon;
		#region Private Members
		private int ImgNewsId;
		#endregion

		#region Event Handlers
		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{
				ImgNewsController objCtlImgNews = new ImgNewsController();
				ImgNewsInfo objImgNews = new ImgNewsInfo();
				if (!(Request.Params["ImgNewsId"] == null)) 
				{
					ImgNewsId = Int32.Parse(Request.Params["ImgNewsId"]);
				} 
				else 
				{
					ImgNewsId = Null.NullInteger;
				}
				if (!Page.IsPostBack) 
				{
					cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");

					ctlIcon.ShowFiles = true;
                    ctlIcon.ShowTabs = false;
                    ctlIcon.ShowUrls = true;
                    ctlIcon.Required = false;

                    ctlIcon.ShowLog = false;
                    ctlIcon.ShowNewWindow = false;
                    ctlIcon.ShowTrack = false;
                    ctlIcon.FileFilter = DotNetNuke.Common.Globals.glbImageFileTypes;
                    ctlIcon.Width = "275px";
					ctlIcon.UrlType = "F";
					
					txtCreateDate.Text = DateTime.Now.ToString("yyy-MM-dd");
					if (!Null.IsNull(ImgNewsId)) 
					{
						objImgNews = objCtlImgNews.Get(ImgNewsId, ModuleId);
						txtTitle.Text = objImgNews.Title;
						txtAuthor.Text = objImgNews.Author;
						txtLinks.Text = objImgNews.Links;
						ctlIcon.Url = objImgNews.ImgLink;
						if (objImgNews != null) 
						{
							cmdDelete.Visible = true;
						} 
						else 
						{
							Response.Redirect(Globals.NavigateURL(), true);
						}
					}
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try 
			{
				if (Page.IsValid == true) 
				{
					ImgNewsInfo objImgNews = new ImgNewsInfo();
					objImgNews = ((ImgNewsInfo)CBO.InitializeObject(objImgNews, typeof(ImgNewsInfo)));
					objImgNews.ModuleId = ModuleId;
					objImgNews.Title = txtTitle.Text;
					objImgNews.Author = txtAuthor.Text;
					objImgNews.IP = Request.ServerVariables.Get("REMOTE_ADDR");
					objImgNews.ImgLink =ctlIcon.Url;	//	 txtLinks.Text;
					
					objImgNews.CreateByUser = UserId.ToString();
					objImgNews.CreateDate = DateTime.Now;
//					objImgNews.ImgLink = Article.gdlmoGlobal.GetFileName(flContent);
					ImgNewsController objCtlImgNews = new ImgNewsController();
					if (Null.IsNull(ImgNewsId)) 
					{
//						objCtlImgNews.Add(objImgNews,flContent);
						objCtlImgNews.Add(objImgNews);
					} 
					else 
					{
						objCtlImgNews.Update(objImgNews);
					}
					Response.Redirect(Globals.NavigateURL(), true);
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			try 
			{
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try 
			{
				if (!Null.IsNull(ImgNewsId)) 
				{
					ImgNewsController objCtlImgNews = new ImgNewsController();
					objCtlImgNews.Delete(ImgNewsId);
				}
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		#endregion
	}
}